#define _CRT_SECURE_NO_WARNINGS 1

//https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/

class Solution {
    int m, n, k;
    int ret = 0;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool check[101][101];
public:
    int wardrobeFinishing(int _m, int _n, int _cnt) {
        m = _m, n = _n, k = _cnt;
        dfs(0, 0);
        return ret;
    }

    void dfs(int i, int j)
    {
        ret++;
        check[i][j] = true;
        for (int z = 0; z < 4; z++)
        {
            int x = i + dx[z], y = j + dy[z];
            if (x >= 0 && x < m && y >= 0 && y < n && !check[x][y] && dig(x, y))
            {
                dfs(x, y);
            }
        }
    }

    bool dig(int x, int y)
    {
        int tmp = 0;
        while (x)
        {
            tmp += x % 10;
            x /= 10;
        }
        while (y)
        {
            tmp += y % 10;
            y /= 10;
        }
        return tmp <= k;
    }
};